package view;

import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Point;
import java.awt.Font;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

import model.CriminalRegModel;

import common.loadData;
import dao.Criminal_RegDao;

public class panelMostWanted extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel panelTitle = null;
	private JLabel lbtitle = null;
	private JScrollPane ScrollPaneMostwanted = null;
	private JTable TableMostwanted = null;
	private JButton ButtonAdd = null;
	private JButton ButtonModify = null;
	private JButton ButtonDelete = null;
	public static int IdCriminal = -2;

	JFrame a;
	public panelMostWanted(JFrame a) {
		super();
		this.a = a;
		initialize();
	}

	private void initialize() {
		this.setSize(735, 520);
		this.setLayout(null);
		this.add(getPanelTitle(), null);
		this.add(getScrollPaneMostwanted(), null);
		this.add(getButtonAdd(), null);
		this.add(getButtonModify(), null);
		this.add(getButtonDelete(), null);
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			lbtitle = new JLabel();
			lbtitle.setLocation(new Point(0, 0));
			lbtitle.setFont(new Font("Dialog", Font.BOLD, 24));
			lbtitle.setForeground(new Color(0, 153, 153));
			lbtitle.setText("List Most Wanted");
			lbtitle.setSize(new Dimension(205, 34));
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelTitle.setLocation(new Point(248, 15));
			panelTitle.setSize(new Dimension(206, 33));
			panelTitle.add(lbtitle, null);
		}
		return panelTitle;
	}

	private JScrollPane getScrollPaneMostwanted() {
		if (ScrollPaneMostwanted == null) {
			ScrollPaneMostwanted = new JScrollPane();
			ScrollPaneMostwanted.setLocation(new Point(44, 59));
			ScrollPaneMostwanted.setSize(new Dimension(646, 389));
			ScrollPaneMostwanted.setViewportView(getTableMostwanted());
		}
		return ScrollPaneMostwanted;
	}

	private JTable getTableMostwanted() {
			TableMostwanted = new JTable(loadData.rowmostwanted, loadData.CollunmCriminal);
			if(TableMostwanted.getRowCount()==0)
			{
				JOptionPane.showMessageDialog(null, "No data for keyword '"+mainForm.search+"'!");
			}
		return TableMostwanted;
	}
	private JButton getButtonAdd() {
		if (ButtonAdd == null) {
			ButtonAdd = new JButton();
			ButtonAdd.setIcon(new ImageIcon(getClass().getResource("/image/Create.png")));
			ButtonAdd.setText("Add");
			ButtonAdd.setLocation(new Point(353, 465));
			ButtonAdd.setSize(new Dimension(100, 33));
			ButtonAdd.setMnemonic(KeyEvent.VK_UNDEFINED);
			ButtonAdd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					IdCriminal=-1;
					addCriminal add = new addCriminal();
					add.setVisible(true);
					a.dispose();
				}
			});
		}
		return ButtonAdd;
	}

	private JButton getButtonModify() {
		if (ButtonModify == null) {
			ButtonModify = new JButton();
			ButtonModify.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonModify.setIcon(new ImageIcon(getClass().getResource("/image/Modify.png")));
			ButtonModify.setText("Modify");
			ButtonModify.setLocation(new Point(471, 466));
			ButtonModify.setSize(new Dimension(100, 33));
			ButtonModify.setHorizontalAlignment(SwingConstants.LEFT);
			ButtonModify.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if((TableMostwanted.getSelectedRow())>=0)
					{
					int row = TableMostwanted.getSelectedRow();
					int column = 0;
					IdCriminal = Integer.parseInt(TableMostwanted.getValueAt(row, column).toString());
					addCriminal add = new addCriminal();
					add.setTitle("Update Infomation for Criminal");
					add.setVisible(true);
					a.dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Choice row!");
					}
				}
			});
		}
		return ButtonModify;
	}

	private JButton getButtonDelete() {
		if (ButtonDelete == null) {
			ButtonDelete = new JButton();
			ButtonDelete.setHorizontalTextPosition(SwingConstants.RIGHT);
			ButtonDelete.setIcon(new ImageIcon(getClass().getResource("/image/Delete.png")));
			ButtonDelete.setText("Delete");
			ButtonDelete.setSize(new Dimension(100, 33));
			ButtonDelete.setLocation(new Point(589, 466));
			ButtonDelete.setHorizontalAlignment(SwingConstants.LEFT);
			if(login.idrole==1)
			{
				ButtonDelete.setEnabled(false);
			}
			ButtonDelete.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int row = TableMostwanted.getSelectedRow();
					int column = 0;
					IdCriminal = Integer.parseInt(TableMostwanted.getValueAt(row, column).toString());
					CriminalRegModel model = new CriminalRegModel();
					model.setId(IdCriminal);
					int rsms = JOptionPane.showConfirmDialog(null, "Do you want to delete this Criminal?");
					if(rsms==0)
					{
						int rs=Criminal_RegDao.deleteCriminalReg(model);
						if(rs==1)
						{
							JOptionPane.showMessageDialog(null,"Delete User Criminal!");
							a.dispose();
							loadData.tab=7;
							(new mainForm()).setVisible(true);
						}
						else
						{
							JOptionPane.showMessageDialog(null,"Delete Criminal fail!");
						}
						
					}
				}
			});
		}
		return ButtonDelete;
	}

}
